package com.etc.emsys.dao.emp;

import com.etc.emsys.dao.JDBCutil;
import com.etc.emsys.domain.DeptPojo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeptDao {
    public List<DeptPojo> selectAllDept() {

        List<DeptPojo> dlist = new ArrayList<>();
        try (Connection conn = JDBCutil.getConnection();){
            String sql = "select deptno, dname from dept ";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            //执行sql语句
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                DeptPojo pojo = new DeptPojo();
                dlist.add(pojo);
                pojo.setDeptno(rs.getInt(1));
                pojo.setDname(rs.getString(2));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return dlist;
    }
}
